
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable, TableDropdown } from '@ant-design/pro-components';
import { useEffect, useRef, useState } from 'react';
import request from 'umi-request';
import axios from "axios";
import { Button, Card } from 'antd';
export const waitTimePromise = async (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

export const waitTime = async (time: number = 100) => {
  await waitTimePromise(time);
};
type GithubIssueItem = {
  name: string;
  room: string;
  type: string;
  faka: string;

  state: string;
  comments: number;
  created_at: string;
  updated_at: string;
  closed_at?: string;
};

const useLogcolumns: any = [  
  {  
    dataIndex: 'inedx',  
    valueType: 'index',  
    width: 48,  
  },  
  {  
    title: '控制器ID',  
    dataIndex: 'KongID',  
    copyable: true,  
    ellipsis: true,  
    search: false,   
  },  
  {  
    title: '控制器IP',  
    dataIndex: 'ip',  
    ellipsis: true,  
    search: false,   
  },  
  {  
    title: 'Mac地址',  
    dataIndex: 'mac',  
    ellipsis: true,  
  },  
  {  
    title: '控制器状态',  
    dataIndex: 'xian',  
    ellipsis: true,  
    
    valueType: 'select',  
    valueEnum: {  
      open: {  
        text: '已在线',  
        status: 'Error',  
      },  
      processing: {  
        text: '不在线',  
        status: 'Processing',  
      },  
    },  
  },  
  {  
    title: '备注',  
    dataIndex: 'beu',  
    search: false, 
    sorter: (a: { createdAt: number; }, b: { createdAt: number; }) => a.createdAt - b.createdAt,
  },  
  {  
    title: '操作',  
    valueType: 'option',  
    key: 'option',  
    align: 'center',
    width: 140,  
    render: (text: any, record: { url: string | undefined; }, _: any, action: any) => [  
      <a key="editable" onClick={() => handleEdit('edit', record)}>编辑</a>,
      <a key='kong' onClick={() => handleEdit('del', record)}>控制器重连</a>,
    ],  
  },  
];
const handleEdit = (key: string|null, record: any)=>{
console.log(key)
}
const index:React.FC = () => {
  const [list,setlist] = useState()
  const [btn1Flag, setBtn1Flag] = useState(false);
  const [btn2Flag, setBtn2Flag] = useState(true);

  useEffect(()=>{
    changeK(1)
  },[])
  const changeK = (id:number)=>{
    axios.post('/api/getzaix',{id:id}).then(({data})=>{
      console.log(data)
      setlist(data.list)
      })
  }
  const actionRef = useRef<ActionType>();
  const headertitle = (
    <div style={{marginTop:'10px'}}></div>
  )
  const [sizelist,setsizelist] = useState('default')
  function handleSizeChange(size: any): void {
    setsizelist(size)
  }
  return (
    <>
      <Card style={{marginBottom:"15px",}}>
        
      <Button
        type="primary"
        ghost={btn1Flag}
        onClick={() => {
          setBtn1Flag(!btn1Flag);
          setBtn2Flag(true);
          changeK(1)
        }}
      >
        搜索控制器
      </Button>
        &emsp;
      <Button
        type="primary"
        ghost={btn2Flag}
        onClick={() => {
          changeK(2)
          setBtn2Flag(!btn2Flag);
          setBtn1Flag(true);
        }}
      >
        搜索不在线控制器
      </Button>
        </Card> 
     <ProTable<any>
      columns={useLogcolumns}
      actionRef={actionRef}
      cardBordered
      dataSource={list}
      editable={{
        type: 'multiple',
      }}
      columnsState={{
        persistenceKey: 'useRoom_log',
        persistenceType: 'localStorage',
        onChange(value) {
     
        },
      }}
      rowKey="id"
      search={false}
      size={sizelist}
      onSizeChange={handleSizeChange}
      options={{
        setting: {
          listsHeight: 400,
        },
      }}
      form={{
        // 由于配置了 transform，提交的参与与定义的不同这里需要转化一下
        syncToUrl: (values, type) => {
          if (type === 'get') {
            return {
              ...values,
              created_at: [values.startTime, values.endTime],
            };
          }
          return values;
        },
      }}
      pagination={{
        pageSize: 10,
        onChange: (page) => console.log(page),
      }}
      dateFormatter="string"
      // headerTitle="高级表格"

    />
    </>
  )
}

export default index
  